<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
  <div class="addBrand-container" id="member-add-app">
    <div class="container">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        :inline="false"
        label-width="180px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户编码:" prop="usercode">
          <el-input v-model="ruleForm.usercode" placeholder="请输入用户编码" maxlength="20"/>
        </el-form-item>
        <el-form-item
          label="用户名称:"
          prop="username"
        >
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入用户名称"
            maxlength="20"
          />
        </el-form-item>

        <el-form-item
                label="性别:"
                prop="gender"
        >
          <el-radio-group v-model="ruleForm.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
            <el-radio label="保密"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
                label="出生日期"
                prop="birthday"

        >

          <div class="block">
            <el-date-picker
                    v-model="ruleForm.birthday"
                    type="date"
                    placeholder="选择日期">
            </el-date-picker>
          </div>
        </el-form-item>

        <el-form-item
                label="电话:"
                prop="phone"
        >
          <el-input
                  v-model="ruleForm.phone"
                  placeholder="请输入电话"
                  maxlength="20"
          />
        </el-form-item>

        <el-form-item
                label="地址:"
                prop="address"
        >
          <el-input
                  v-model="ruleForm.address"
                  placeholder="请输入地址"
                  maxlength="50"
          />
        </el-form-item>

        <el-form-item
                label="工作照片:"
                prop="idpicpath"
                class="uploadImg"

        >
          <el-upload
                  class="avatar-uploader"
                  action="/common/upload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :on-change="onChange"
                  ref="upload"
          >
            <img
                    v-if="imageUrl"
                    :src="imageUrl"
                    class="avatar"
            ></img>
            <i
                    v-else
                    class="el-icon-plus avatar-uploader-icon"
            ></i>
          </el-upload>
        </el-form-item>

        <el-form-item
                label="用户角色:"
                prop="userrole"
        >
          <el-select
                  v-model="ruleForm.userrole"
                  placeholder="请选择用户角色"
          >
            <el-option v-for="(item,index) in dishList" :key="index" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>

        <div class="subBox address">
          <el-form-item>
            <el-button  @click="goBack1()">
              取消
            </el-button>
            <el-button
              type="primary"
              @click="submitForm('ruleForm', false)"
            >
              保存
            </el-button>
            <el-button
              v-if="actionType == 'add'"
              type="primary"
              class="continue"
              @click="submitForm('ruleForm', true)"
            >
              保存并继续添加
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../api/user.js"></script>
    <script src="../../js/validate.js"></script>
    <script src="../../js/index.js"></script>
    <script>
      new Vue({
        el: '#member-add-app',
        data() {
          return {
            id: '',
            actionType : '',
            imageUrl: '',
            dishList: [  {id: 1, name: '管理员'},
              {id: 2, name: '普通用户'},
              {id: 3, name: '游客'}],
            ruleForm : {
                usercode:'',
                username: '',
                'gender': '保密',
                'birthday': '',
                'phone': '',
                'idpicpath': '',
                'address': '',
                'userrole': '普通用户'
            }
          }
        },
        computed: {
          rules () {
            return {
              usercode: [
                {
                  required: true, 'validator': checkUserName, trigger: 'blur'
                }
              ],
              //姓名
              username: [{ required: true, 'validator': checkName, 'trigger': 'blur' }],
            }
          }
        },
        created() {
          this.id = requestUrlParam('id')
          this.actionType = this.id ? 'edit' : 'add'
          if (this.id) {
            this.init()
          }
        },
        mounted() {
        },
        methods: {
          async init () {
            queryEmployeeById(this.id).then(res => {
              console.log(res)
              if (String(res.code) === '1') {
                console.log(res.data)
                console.log(res.birthday)
                this.ruleForm = res.data
                this.imageUrl = `/common/download?name=${res.data.idpicpath}`
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          },
          submitForm (formName, st) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                if(!this.imageUrl){
                  this.$message.error('请上传工作图片')
                  return
                }
                if (this.actionType === 'add') {
                  const params = {
                    ...this.ruleForm,
                     }
                  addUser(params).then(res => {
                    console.log(this.birthday)
                    if (res.code === 1) {
                      this.$message.success('用户添加成功！')
                      if (!st) {
                        this.goBack1()
                      } else {
                        this.imageUrl = ''
                        this.ruleForm = {
                          usercode:'',
                          username: '',
                          'idpicpath': '',
                          'gender': '保密',
                          'birthday': '',
                          'phone': '',
                          'address': '',
                          'userrole': '普通用户'
                        }
                      }
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
                } else {
                  const params = {
                    ...this.ruleForm,
                   }
                  editUser(params).then(res => {
                    console.log(this.birthday)
                    if (res.code === 1) {
                      this.$message.success('员工信息修改成功！')
                      this.goBack1()
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
                }
              } else {
                console.log('error submit!!')
                return false
              }
            })
          },


          handleAvatarSuccess (response, file, fileList) {
            // 拼接down接口预览
            if(response.code === 0 && response.msg === '未登录'){
              window.top.location.href = '/backend/page/login/login.html'
            }else{
              this.imageUrl = `/common/download?name=${response.data}`
              this.ruleForm.idpicpath = response.data
            }
          },

          onChange (file) {
            if(file){
              const suffix = file.name.split('.')[1]
              const size = file.size / 1024 / 1024 < 2
              if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                this.$refs.upload.clearFiles()
                return false
              }
              if(!size){
                this.$message.error('上传文件大小不能超过 2MB!')
                return false
              }
              return file
            }
          },

          goBack1(){
            window.parent.menuHandle({
              id: '3',
              url: '/backend/page/user/list.html',
              name: '员工管理'
            },false)
          }

        }
      })
    </script>
</body>
</html>